package com.cci.kangdao.escalation.aop;

import com.cci.kangdao.common.type.NotifyChannelType;
import com.cci.kangdao.common.type.NotifyModelType;
import com.cci.kangdao.crmapi.CRMApiUtils;
import com.cci.kangdao.escalation.aop.model.EscalationThreadLocal;
import com.cci.kangdao.escalation.dao.EscalationOrderTDao;
import com.cci.kangdao.escalation.model.EscalationOrderT;
import com.cci.kangdao.escalation.model.type.TypeDictionary;
import com.cci.kangdao.escalation.service.EscalationNotifyService;
import com.cci.kangdao.service.NotifyConfigService;
import com.cci.kangdao.service.impl.NotifyConfigServiceImpl;
import com.cci.kangdao.utilTool.NumberUtils;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author William Du
 * @date 2020/5/6
 */
@Component
@Aspect
@Order
public class EscalationIntercept {

    @Resource
    private EscalationNotifyService escalationNotifyService;

    @Resource
    private NotifyConfigService notifyConfigService;

    @Resource
    private EscalationOrderTDao escalationOrderTDao;

    @Around("(execution(* com.cci.kangdao.escalation.service.EscalationService.*(..)))")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        try {
            Object response = null;
            response =  proceedingJoinPoint.proceed();
            Long escalationOrderId = EscalationThreadLocal.get();
            String className = proceedingJoinPoint.getSignature().getDeclaringType().getSimpleName();
            String methodName = proceedingJoinPoint.getSignature().getName();
            if(NumberUtils.isPositiveNum(escalationOrderId)){
                notifyConfigService.doNotify(NotifyChannelType.CCI.getCode(), NotifyModelType.ESCALATION.getCode(),className,methodName,escalationOrderId);
                // 3.3.0 当升级工单进入升级完成的状态时，需要将详情信息同步到CRM - 异步处理 - 暂不开放，业务王彦腾，前端王恒泰
//                EscalationOrderT escalationOrderT = escalationOrderTDao.selectByPrimaryKey(escalationOrderId);
//                if (escalationOrderT.getStatus().equals(TypeDictionary.ORDERTYPE_FINISHED.getCode())){
//                    CRMApiUtils.getCRMServiceApi().syncEscalationOrderDetail(escalationOrderT, 0);
//                }
            }
            return response;
        } finally {
            EscalationThreadLocal.clear();
        }
    }

}
